import { store } from "@/stores"
import { type DerivativeFunc, theme } from "ant-design-vue"
import type { AliasToken, MapToken, SeedToken } from "ant-design-vue/es/theme/interface"
import { defineStore } from "pinia"
import type { AppState, ThemeType } from "./types/AppState"

export const useAppStore = defineStore("antdv-app", {
  state: (): AppState => ({
    theme: "default",
    //主题色
    colorPrimary: "#00b96b",
  }),
  actions: {
    /**
     * 获取主题Token
     */
    getThemeToken(): ComputedRef<AliasToken> {
      return theme.useToken().token
    },
    /**
     * 设置主题Token
     * @param themeType 主题类型
     */
    setThemeToken(themeType: ThemeType) {
      this.theme = themeType
    },
    /**
     * 设置主题颜色
     * @param color 主题颜色
     */
    setcolorPrimary(color: string) {
      this.colorPrimary = color
    },
    /**
     * 根据主题类型获取主题算法
     */
    getByTypeTheme(type: ThemeType): DerivativeFunc<SeedToken, MapToken> {
      const themeMap = {
        default: theme.defaultAlgorithm,
        dark: theme.darkAlgorithm,
        compact: theme.compactAlgorithm,
      }
      return themeMap[type]
    },
  },
  persist: {
    key: "education-web-app",
    storage: localStorage,
  },
})

/**
 * 获取主题Token
 */
export function getThemeTokenHook(): ComputedRef<AliasToken> {
  return useAppStore(store).getThemeToken()
}

/**
 * 获取AppStore
 */
export function useAppStoreHook() {
  return useAppStore(store)
}
